package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"srv/model/mysql"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *srv.UserUpdateRequest) (*srv.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	u := mysql.User{}
	err := u.UserInfoId(in.UserId)
	if err != nil {
		return nil, fmt.Errorf("用户不存在")
	}
	user := mysql.User{
		Model:    gorm.Model{ID: u.ID},
		Name:     u.Name,
		Tel:      u.Tel,
		Password: u.Password,
		AddTime:  u.AddTime,
		UserType: int(in.UserType),
	}
	mysql.DB.Updates(&user)

	return &srv.UserUpdateResponse{
		UserId:   int64(user.ID),
		Name:     user.Name,
		Tel:      user.Tel,
		AddTime:  user.AddTime,
		UserType: int64(user.UserType),
	}, nil
}
